package com.qx.ddx.activity.fragment;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qx.ddx.R;
import com.qx.ddx.activity.SetActivity;
import com.qx.ddx.activity.fragment.adapter.ThemeAdapter;
import com.qx.ddx.util.SpInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/6/24.
 */
public class ThemeFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_set_theme, container, false);
        RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.set_theme_recyclerView);
        GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
        mLayoutManager.setAutoMeasureEnabled(false);
        mRecyclerView.setHasFixedSize(false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        ThemeAdapter adapter = new ThemeAdapter();
        adapter.setData(getData(adapter));
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new ThemeAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int index, int itemBgID) {
                SharedPreferences sp = getActivity().getSharedPreferences(SpInfo.Setting, Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putInt(SpInfo.ThemeIndex, index);
                switch (itemBgID) {
                    case R.mipmap.theme_item_bg_1:
                        editor.putInt(SpInfo.ThemeBg, R.mipmap.theme_bg_1);
                        ((SetActivity) getActivity()).view.setBackgroundResource(R.mipmap.theme_bg_1);
                        break;
                    case R.mipmap.theme_item_bg_2:
                        editor.putInt(SpInfo.ThemeBg, R.mipmap.theme_bg_2);
                        ((SetActivity) getActivity()).view.setBackgroundResource(R.mipmap.theme_bg_2);
                        break;
                    case R.mipmap.theme_item_bg_3:
                        editor.putInt(SpInfo.ThemeBg, R.mipmap.theme_bg_3);
                        ((SetActivity) getActivity()).view.setBackgroundResource(R.mipmap.theme_bg_3);
                        break;
                    case R.mipmap.theme_item_bg_4:
                        editor.putInt(SpInfo.ThemeBg, R.mipmap.theme_bg_4);
                        ((SetActivity) getActivity()).view.setBackgroundResource(R.mipmap.theme_bg_4);
                        break;
                    case R.mipmap.theme_item_bg_5:
                        editor.putInt(SpInfo.ThemeBg, R.mipmap.theme_bg_5);
                        ((SetActivity) getActivity()).view.setBackgroundResource(R.mipmap.theme_bg_5);
                        break;
                    case R.mipmap.theme_item_bg_6:
                        editor.putInt(SpInfo.ThemeBg, R.mipmap.theme_bg_6);
                        ((SetActivity) getActivity()).view.setBackgroundResource(R.mipmap.theme_bg_6);
                        break;
                    default:
                        editor.putInt(SpInfo.ThemeBg, R.color.set_background);
                        ((SetActivity) getActivity()).view.setBackgroundResource(R.color.set_background);
                        break;
                }
                editor.commit();
            }
        });
        return view;
    }

    private List<HashMap<String, String>> getData(ThemeAdapter adapter) {
        List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
        int[] bgs = new int[]{R.color.set_background, R.mipmap.theme_item_bg_1, R.mipmap.theme_item_bg_2,
                R.mipmap.theme_item_bg_3, R.mipmap.theme_item_bg_4, R.mipmap.theme_item_bg_5, R.mipmap.theme_item_bg_6};
        String theme_default = getString(R.string.set_theme_default);
        String theme_halo = getString(R.string.set_theme_halo);
        String theme_colorful = getString(R.string.set_theme_colourful);
        String[] names = new String[]{theme_default, theme_halo, theme_colorful, theme_colorful, theme_colorful, theme_colorful, theme_colorful};
        SharedPreferences sp = getActivity().getSharedPreferences(SpInfo.Setting, Activity.MODE_PRIVATE);
        int index = sp.getInt(SpInfo.ThemeIndex, 0);
        adapter.setSelectedIndex(index);
        for (int i = 0; i < names.length; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            if (index == i) {
                map.put("isSelected", "true");
            } else {
                map.put("isSelected", "false");
            }
            map.put("bg", String.valueOf(bgs[i]));
            map.put("name", names[i]);
            data.add(map);
        }
        return data;
    }
}
